package com.online.web.shiro.biz.user;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.online.web.shiro.dao.user.PermissionDao;
import com.online.web.shiro.model.user.PermissionInfo;
import com.online.web.shiro.util.lang.JsonUtil;

/**
 * 权限
 * 
 * @author tongyufu
 *
 */
@Service
public class PermissionBiz {

    @Autowired
    private PermissionDao permissionDao;

    public List<PermissionInfo> search(Map<String, Object> params) {
        List<PermissionInfo> permissions = permissionDao.search(params);
        List<PermissionInfo> tree = new ArrayList<PermissionInfo>();
        for (PermissionInfo root : permissions) {
            if (root.getParentId() == 0) {
                this.createTree(root, permissions);
                tree.add(root);
            }
        }
        return tree;
    }

    /**查询指定用户的权限菜单树*/
    public List<PermissionInfo> queryByUserId(Integer userId) {
        List<PermissionInfo> permissions = permissionDao.queryByUserId(userId);
        List<PermissionInfo> tree = new ArrayList<PermissionInfo>();
        for (PermissionInfo root : permissions) {
            if (root.getParentId() == 0) {
                this.createTree(root, permissions);
                tree.add(root);
            }
        }
        return tree;
    }

    /**生成树*/
    private void createTree(PermissionInfo root, List<PermissionInfo> permissions) {
        for (PermissionInfo leaf : permissions) {
            if (leaf.getParentId() == root.getId()) {
                root.getChildren().add(leaf);
                //权限类型(0:菜单; 1:按钮)
                if ("0".equals(leaf.getType())) {
                    this.createTree(leaf, permissions);
                }
            }
        }
    }

    /**查询指定角色的权限树*/
    @SuppressWarnings("unchecked")
    public List<Map<String, Object>> queryByRole(Integer roleId) {
        List<PermissionInfo> permissions = permissionDao.search(null);
        List<PermissionInfo> rolePermissions = permissionDao.queryByRoleId(roleId);
        List<Map<String, Object>> tree = new ArrayList<>();

        for (PermissionInfo permission : permissions) {
            if (permission.getParentId() == 0) {
                Map<String, Object> root = permission.transToTreeMap();
                this.createTree(root, permissions, rolePermissions);
                //如果有三级菜单，则取消二级菜单的选中属性
                List<Map<String, Object>> childrens = (List<Map<String, Object>>) root
                    .get("children");
                for (Map<String, Object> children : childrens) {
                    List<Map<String, Object>> childs = (List<Map<String, Object>>) children
                        .get("children");
                    if (childs != null && !childs.isEmpty()) {
                        children.remove("checked");
                    }
                }
                tree.add(root);
            }
        }
        return tree;
    }

    @SuppressWarnings("unchecked")
    private void createTree(Map<String, Object> root, List<PermissionInfo> permissions,
                            List<PermissionInfo> rolePermissions) {
        for (PermissionInfo leaf : permissions) {
            Integer rootId = Integer.parseInt(root.get("id").toString());
            if (leaf.getParentId() == rootId) {
                Map<String, Object> map = leaf.transToTreeMap();
                List<Map<String, Object>> children = (List<Map<String, Object>>) root
                    .get("children");

                if (children == null) {
                    children = new ArrayList<>();
                }
                for (PermissionInfo rp : rolePermissions) {
                    if (rp.getId() == leaf.getId()) {
                        map.put("checked", true);
                        break;
                    }
                }
                children.add(map);
                root.put("children", children);
                //权限类型(0:菜单; 1:按钮)
                if ("0".equals(leaf.getType())) {
                    this.createTree(map, permissions, rolePermissions);
                }
            }
        }
    }

    public String save(PermissionInfo permissionInfo) {
        if (permissionInfo.getId() == null) {
            permissionDao.save(permissionInfo);
        } else {
            permissionDao.update(permissionInfo);
        }
        return JsonUtil.toFormJson("操作成功", true);
    }

    @Transactional
    public String delete(Integer id) {
        permissionDao.deleteChild(id);
        permissionDao.delete(id);
        return JsonUtil.toFormJson("操作成功", true);
    }

}
